Requires Scripting PRO
The HealthStatistics class provides an interface for analyzing aggregated health quantity data over a specified time range. It allows you to compute key statistical values such as:
This class is ideal for generating summaries of daily, weekly, or custom health data intervals.
Each HealthStatistics instance represents statistics for a specific HealthQuantityType within a defined startDate and endDate range. You can optionally filter statistics by a HealthSource (e.g., only include samples recorded by a specific device or app).
| Property | Type | Description |
|---|---|---|
quantityType |
HealthQuantityType |
The quantity type the statistics are based on (e.g., stepCount) |
startDate |
Date |
The beginning of the statistics window |
endDate |
Date |
The end of the statistics window |
sources |
HealthSource[] | null |
The list of sources contributing data to these statistics |
duration(unit: HealthUnit, source?: HealthSource): number | nullReturns the total accumulated duration of all samples within the range.
unit: The unit of time to return the duration in (e.g., seconds, minutes).source (optional): If provided, only samples from that source will be included.Returns null if no matching samples are found.
averageQuantity(unit: HealthUnit, source?: HealthSource): number | nullReturns the average quantity value of all samples.
unit: The unit to express the average in (e.g., HealthUnit.bpm()).source (optional): Filter samples by source.sumQuantity(unit: HealthUnit, source?: HealthSource): number | nullReturns the total sum of quantity values over the date range.
minimumQuantity(unit: HealthUnit, source?: HealthSource): number | nullReturns the minimum recorded value in the given unit.
maximumQuantity(unit: HealthUnit, source?: HealthSource): number | nullReturns the maximum recorded value in the given unit.
mostRecentQuantity(unit: HealthUnit, source?: HealthSource): number | nullReturns the most recent quantity value recorded within the range. If no values are available, returns null.
mostRecentQuantityDateInterval(source?: HealthSource): HealthDateInterval | nullReturns a HealthDateInterval object indicating the start and end time of the most recent recorded value. Useful for knowing when the last data point was recorded.
HealthSource ClassThe HealthSource class represents the origin of a HealthKit sample. It is typically an app or device that generated or synced the health data.
| Property | Type | Description |
|---|---|---|
bundleIdentifier |
string |
The app or device bundle ID (e.g., "com.apple.Health") |
name |
string |
A human-readable name for the source |
HealthSource.forCurrentApp(): HealthSourceReturns a HealthSource object representing the current Scripting app. This can be used to filter statistics only for data recorded or synced by your app.
HealthStatistics helps you analyze trends over time by calculating averages, totals, and most recent values.HealthSource.